import os
from PIL import Image, ImageDraw, ImageFont


def init_img(width=394, height=394):
    """创建底图

    Args:
        width (int, optional): 宽. Defaults to 394.
        height (int, optional): 高. Defaults to 394.

    Returns:
        _type_: _description_
    """
    img = Image.new('RGB', (width, height), (255, 255, 255))
    return img


def centered_overlay_product(background: Image.Image, pro_src: str):
    """产品图等比例居中于底图

    Args:
        background (Image.Image): _description_
        pro_src (str): _description_

    Returns:
        _type_: _description_
    """
    original_image = Image.open(pro_src)

    # background = init_img()
    # 获取背景图和原始图像的尺寸
    bg_width, bg_height = background.size
    img_width, img_height = original_image.size

    # 如果原始图像比背景图大，则按比例缩小
    if img_width > bg_width or img_height > bg_height:
        # 计算缩放比例，保持宽高比
        scaling_factor = min(bg_width / img_width, bg_height / img_height)
        new_size = (int(img_width * scaling_factor),
                    int(img_height * scaling_factor))
        original_image = original_image.resize(new_size, Image.LANCZOS)
        img_width, img_height = original_image.size

    # 计算居中位置
    x = (bg_width - img_width) // 2
    y = (bg_height - img_height) // 2

    # 将原始图像粘贴到背景图的居中位置
    background.paste(original_image, (x, y))

    return background


def overlay_logo(logo_src, orginal_image):
    logo_image = Image.open(logo_src)
    # orginal_image = Image.open(orginal_src)

    logo_image = logo_image.resize((394, 394))
    orginal_image.paste(logo_image, (0, 0), mask=logo_image)
    return orginal_image


if __name__ == "__main__":
    # 获取指定目录下的文件列表

    file_list = os.listdir('download')

    for img in file_list:
        try:
            print(f"处理：{img}")
            background_img = init_img()
            pro_img = centered_overlay_product(background_img,
                                               f'download/{img}')
            res = overlay_logo('layout.png', pro_img)
            res.save(f'process/{img}')

        except Exception as e:
            print(e)

    # res.show()
    # # 读取要叠加的图像
    # overlay_image = Image.open('layout.png')

    # # 创建新图像，大小与原始图片一致
    # new_image = Image.new('RGBA', background.size)

    # # 将原始图片粘贴到新图像上
    # new_image.paste(background, (0, 0))

    # # 调整要叠加的图像的大小
    # overlay_image = overlay_image.resize((394, 394))

    # # 计算叠加的位置
    # position = (0, 0)

    # # 将要叠加的图像粘贴到新图像上
    # new_image.paste(overlay_image, position, mask=overlay_image)

    # # 显示叠加后的图像
    # new_image.show()

    # 初始化背景图
    # background = init_img()

    # # 打开原始图像
    # original_image = Image.open('download/829524.jpg')

    # # 获取背景图和原始图像的尺寸
    # bg_width, bg_height = background.size
    # img_width, img_height = original_image.size

    # # 计算居中位置
    # x = (bg_width - img_width) // 2
    # y = (bg_height - img_height) // 2

    # # 将原始图像粘贴到背景图的居中位置
    # background.paste(original_image, (x, y))

    # # 保存或显示结果图像
    # background.show()  # 显示图像
